package com.yssoft.service.supervise;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yssoft.common.Constants;
import com.yssoft.entity.core.TEnterprise;
import com.yssoft.entity.supplier.BillCountCustom;
import com.yssoft.entity.supplier.ProductCountCustom;
import com.yssoft.mapper.supplier.TProductCountMapper;
import com.yssoft.utils.LayuiUtil;
import com.yssoft.utils.UserHelper;

@Service
public class ProductCountService {
	@Resource
	private TProductCountMapper productCountMapper;
	public JSONObject queryProductPage(int page, int limit, ProductCountCustom custom) throws IllegalAccessException, ClassNotFoundException {
	 
        PageHelper.startPage(page,limit);//下面第一个查询结果进行分页
        //当参数为空处理，防止空指针报错
        if(custom==null ) {
        	custom=new ProductCountCustom();
        }
        //溯源类型和数据有效性
        custom.setDeleteFlag("0");
        custom.setClassify("traceType");
        List<TEnterprise> eList=null;
        if(StringUtils.isNoneBlank(custom.getJsonParam())) {
        	String json=custom.getJsonParam();
            eList=JSONArray.parseArray(json, TEnterprise.class);
        }
        //区域为全国时
        if(null != custom.getAreaId() && custom.getAreaId().equals("100000")){
        	custom.setAreaId(null);
        	custom.setIsNationwide("y");
        }
        //查询分页数据
        List<ProductCountCustom> list =productCountMapper.selectProductStatisticsPage(custom,eList);
        //查询总条数
        int count = productCountMapper.selectProductStatisticsPageCount(custom,eList);
        JSONObject jsonObject =LayuiUtil.getJSONObjectByObject(list,count,"","");
        return jsonObject;
	}
	/**
	 * 
	    * @Title: querymaterialByProductId
	    * @Description: 根据产品id查询产品
	    * @param @param productId
	    * @param @return    参数
	    * @return Map<String,Object>    返回类型
	    * @throws
	 */
	public Map<String, Object> querymaterialByProductId(Integer productId) {
		String  splitStr="、";//当前分隔符
		String rawClassify="";//原料
		String packagingClassify="";//包材
		String accessoriesClassify="";//辅料
		String additiveClassify="";//添加剂
		//相关物料
		Map<String, Object> param=new HashMap<>();
		param.put("productId", productId);
    	List<ProductCountCustom>   materialList=productCountMapper.selectMaterialList(null,param);
    	for (ProductCountCustom  material:materialList) {
			//类别（原料类别  rawClassify  包材 packagingClassify  添加剂 additiveClassify   辅料accessoriesClassify）
			String  classify=material.getClassify().toString();
			String  materiaName=material.getMateriaName();
			if(  Constants.D_RAW_CLASSIFY.equals( classify )   ) {
				rawClassify=rawClassify+materiaName+splitStr;
			}
			if( Constants.D_ACCESSORIES_CLASSIFY.equals( classify)   ) {
				accessoriesClassify=accessoriesClassify+materiaName+splitStr;
			}
			if( Constants.D_ADDITIVE_CLASSIFY.equals(classify )   ) {
				additiveClassify=additiveClassify+ materiaName+splitStr;
			}
			if( Constants.D_PACKAGING_CLASSIFY.equals( classify)   ) {
				packagingClassify=packagingClassify+materiaName+splitStr;
			}
	
		}
		if(StringUtils.isNoneBlank(rawClassify)) {
			rawClassify=rawClassify.substring(0, rawClassify.lastIndexOf(splitStr)) ;
		}
		if(StringUtils.isNoneBlank(accessoriesClassify)) {
			accessoriesClassify=accessoriesClassify.substring(0, accessoriesClassify.lastIndexOf( splitStr ));		
		}
		if(StringUtils.isNoneBlank(additiveClassify)) {
			additiveClassify=additiveClassify.substring(0, additiveClassify.lastIndexOf( splitStr ));
		}
		if(StringUtils.isNoneBlank(packagingClassify)) {
			packagingClassify=packagingClassify.substring(0, packagingClassify.lastIndexOf( splitStr ));
		}
		 
    	Map<String, Object> map=new HashMap<>();
    	map.put("rawClassify",rawClassify);
    	map.put("packagingClassify", packagingClassify);
    	map.put("accessoriesClassify", accessoriesClassify);
    	map.put("additiveClassify", additiveClassify);
		return map;
	}
	
}
